package com.wz.project.kpi.service.impl;


import com.wz.device.entity.BasCargoSpace;
import com.wz.device.mapper.BasCargoSpaceMapper;
import com.wz.project.cxtj.inOrder.domain.ViewInOrder;
import com.wz.project.cxtj.inOrder.mapper.ViewInOrderMapper;
import com.wz.project.cxtj.outOrder.domain.ViewOutOrder;
import com.wz.project.cxtj.outOrder.mapper.ViewOutOrderMapper;
import com.wz.project.kpi.service.KpiService;
import com.wz.project.mydo.domain.MydoJobFinish;
import com.wz.project.mydo.mydojob.mapper.MydoJobFinishMapper;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.stock.mapper.BasStockMapper;
import com.wz.project.stock.stockdetail.domain.BasStockDetail;
import com.wz.project.stock.stockdetail.mapper.BasStockDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.View;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class KpiServiceImpl implements KpiService {

   @Autowired
   private BasCargoSpaceMapper basCargoSpaceMapper;

   @Autowired
   private BasStockMapper basStockMapper;

   @Autowired
   private MydoJobFinishMapper jobFinishJMapper;

   @Autowired
   private ViewInOrderMapper viewInOrderMapper;
   @Autowired
   private ViewOutOrderMapper viewOutOrderMapper;

   @Autowired
   private BasStockDetailMapper basStockDetailMapper;


   @Override
   public Map<String, Long> getCargoSpaceAnalysis() {
      Map<String, Long> map = new HashMap<>();
      for (int i = 1; i < 7; i++) {
         Integer status = basCargoSpaceMapper.findByStatus(i + "");
         map.put("s" + i, Long.valueOf(status));
      }
      return map;
   }
   @Override
   public Map<String, Long> getCargoSpaceAnalysis1() {
      Map<String, Long> map = new HashMap<>();
      for (int i = 1; i < 7; i++) {
         Integer status = basCargoSpaceMapper.findByStatus1(i + "");
         map.put("s" + i, Long.valueOf(status));
      }
      return map;
   }

   @Override
   public Map<String, Long> getCargoSpaceAnalysis2() {
      Map<String, Long> map = new HashMap<>();
      for (int i = 1; i < 7; i++) {
         Integer status = basCargoSpaceMapper.findByStatus2(i + "");
         map.put("s" + i, Long.valueOf(status));
      }
      return map;
   }

   @Override
   public Map<String, Object[]> getCargoSpacePlan(int roadway, int platoon) {

      BasCargoSpace space1 = new BasCargoSpace();
      BasCargoSpace space2 = new BasCargoSpace();
      BasCargoSpace space3 = new BasCargoSpace();
      BasCargoSpace space4 = new BasCargoSpace();
      BasCargoSpace space5 = new BasCargoSpace();
      BasCargoSpace space6 = new BasCargoSpace();
      space1.setRoadway_id(String.valueOf(roadway));
      space1.setThe_platoon(platoon);
      space1.setStatus("1");
      space2.setRoadway_id(String.valueOf(roadway));
      space2.setThe_platoon(platoon);
      space2.setStatus("2");
      space3.setRoadway_id(String.valueOf(roadway));
      space3.setThe_platoon(platoon);
      space3.setStatus("3");
      space4.setRoadway_id(String.valueOf(roadway));
      space4.setThe_platoon(platoon);
      space4.setStatus("4");
      space5.setRoadway_id(String.valueOf(roadway));
      space5.setThe_platoon(platoon);
      space5.setStatus("5");
      space6.setRoadway_id(String.valueOf(roadway));
      space6.setThe_platoon(platoon);
      space6.setStatus("6");
      List<String> cargo1 = basCargoSpaceMapper.findByThePlatoonAndStatus(space1);
      List<String[]> cargo11 =new ArrayList<>();
      if (cargo1!=null && cargo1.size()!=0){
         for (String s : cargo1) {
            String[] split = s.split(",");
            cargo11.add(split);
         }
      }
      List<String> cargo2 = basCargoSpaceMapper.findByThePlatoonAndStatus(space2);
      List<String[]> cargo22 = new ArrayList<>();
      if (cargo2!=null && cargo2.size()!=0){
         for (String s : cargo2) {
            String[] split = s.split(",");

            cargo22.add(split);

         }}
      List<String> cargo3 = basCargoSpaceMapper.findByThePlatoonAndStatus(space3);
      List<String[]> cargo33 =  new ArrayList<>();
      if (cargo3!=null && cargo3.size()!=0){
         for (String s : cargo3) {
            String[] split = s.split(",");

            cargo33.add(split);

         }}
      List<String> cargo4 = basCargoSpaceMapper.findByThePlatoonAndStatus(space4);
      List<String[]> cargo44 =  new ArrayList<>();
      if (cargo4!=null && cargo4.size()!=0){
         for (String s : cargo4) {
            String[] split = s.split(",");

            cargo44.add(split);

         }}
      List<String> cargo5 = basCargoSpaceMapper.findByThePlatoonAndStatus(space5);
      List<String[]> cargo55 =  new ArrayList<>();
      if (cargo5!=null && cargo5.size()!=0){
         for (String s : cargo5) {
            String[] split = s.split(",");

            cargo55.add(split);

         }}
      List<String> cargo6 = basCargoSpaceMapper.findByThePlatoonAndStatus(space6);
      List<String[]> cargo66 =  new ArrayList<>();
      if (cargo6!=null && cargo6.size()!=0){
         for (String s : cargo6) {
            String[] split = s.split(",");

            cargo66.add(split);
         }
      }


      Object[] empty = cargo11.toArray();
      Object[] full = cargo22.toArray();
      Object[] inAllot = cargo33.toArray();
      Object[] outAllot = cargo44.toArray();
      Object[] inCheck = cargo55.toArray();
      Object[] outCheck = cargo66.toArray();
      Integer[] floor = basCargoSpaceMapper.findByTheFloorDistinct();
      Integer[] column = basCargoSpaceMapper.findByTheColumnDistinct();

      Map<String, Object[]> map = new HashMap<>();
      map.put("emp", empty);
      map.put("fu", full);
      map.put("inAllot", inAllot);
      map.put("outAllot", outAllot);
      map.put("inCheck", inCheck);
      map.put("outCheck", outCheck);
      map.put("flo", floor);
      map.put("col", column);
      return map;
   }

   @Override
   public Map<String, String> getMessage(Integer platoon, Integer column, Integer floor, Integer row) {
      BasCargoSpace space = new BasCargoSpace();
      space.setThe_platoon(platoon);
      space.setThe_column(column);
      space.setThe_floor(floor);
      String msg = null;
      List<BasStock> list = basStockMapper.findByThePlatoonAndTheColumnAndTheFloor(space);
      if (list.size()>0 && list!=null){
         for (BasStock basStock : list) {
            msg = "托盘号:" + basStock.getTrayCode()
                    + "品名:" + basStock.getProductName()
                    + "数量:" + basStock.getQuantity()
                    + "货位类型:" + basStock.getThe_type()+"<br>";
         }
      }
      Map<String, String> map = new HashMap<>();
      map.put("msg", msg);
      return map;

   }

   @Override
   public Map<String, long[]> getMydojobFinish(String startTime, String finishTime,int a) {
      long[] in = new long[8];
      MydoJobFinish mydoJobFinish = new MydoJobFinish();
      mydoJobFinish.setStartTime(startTime);
      mydoJobFinish.setFinishTime(finishTime);
      mydoJobFinish.setInOrOut(String.valueOf(a));
      if(a<=10) {
         mydoJobFinish.setRoadwayId(String.valueOf(1));
         long inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[0] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(2));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[1] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(3));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[2] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(4));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[3] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(5));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[4] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(6));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[5] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(7));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[6] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(8));
         inLong = jobFinishJMapper.countInFinish(mydoJobFinish);
         in[7] = inLong;
      }else{
         mydoJobFinish.setRoadwayId(String.valueOf(1));
         long inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);

         in[0] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(2));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[1] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(3));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[2] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(4));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[3] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(5));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[4] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(6));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[5] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(7));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[6] = inLong;

         mydoJobFinish.setRoadwayId(String.valueOf(8));
         inLong = jobFinishJMapper.countOutFinish(mydoJobFinish);
         in[7] = inLong;
      }
      Map<String, long[]> map = new HashMap<>();
      map.put("in", in);
      return map;
   }

   @Override
   public Map<String, Integer[]> getMydojobFinishs(String startTime, String finishTime,int a) {
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
      String format = sdf.format(date);
      Integer[] in = new Integer[31];

      if(a<=10) {
         ViewInOrder viewInOrder = new ViewInOrder();
         viewInOrder.setInOrOut(String.valueOf(a));
         for (int i = 0; i < 31 ; i++) {
            int j = i+1;
            if (j<10){
               if (startTime.equals("")){
                  viewInOrder.setStartTime(format+"-0"+j);
               }else {
                  viewInOrder.setStartTime(startTime+"-0"+j);
               }
            }else {
               if (startTime.equals("")){
                  viewInOrder.setStartTime(format+"-"+j);
               }else {
                  viewInOrder.setStartTime(startTime+"-"+j);
               }
            }
            Integer integer = viewInOrderMapper.countInOrder(viewInOrder);
            in[i] = integer;
         }
      }else{
         ViewOutOrder viewOutOrder = new ViewOutOrder();
         for (int i = 0; i < 31 ; i++) {
            int j = i+1;
            if (j<10){
               if (startTime.equals("")){
                  viewOutOrder.setStartTime(format+"-0"+j);
               }else {
                  viewOutOrder.setStartTime(startTime+"-0"+j);
               }
            }else {
               if (startTime.equals("")){
                  viewOutOrder.setStartTime(format+"-"+j);
               }else {
                  viewOutOrder.setStartTime(startTime+"-"+j);
               }
            }

            Integer inLong = viewOutOrderMapper.countOutOrder(viewOutOrder);
            in[i] = inLong;
         }
      }
      Map<String, Integer[]> map = new HashMap<>();
      map.put("in", in);
      return map;
   }

   @Override
   public Map<String, long[]> getStockdetail(String startTime, String finishTime) {
      BasStockDetail basStockDetail = new BasStockDetail();
      if (startTime.equals("") && finishTime.equals("")){
         Date date = new Date();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
         String format = sdf.format(date);
         basStockDetail.setStartTime(format+"00");
      }else {
         basStockDetail.setStartTime(startTime);
         basStockDetail.setFinishTime(finishTime);
      }
      List<BasStockDetail> basStockDetails = basStockDetailMapper.selectBasStockDetailByLoton(basStockDetail);
      long[] in = new long[basStockDetails.size()];
      for (int i = 0; i < basStockDetails.size() ; i++) {
         BasStockDetail basStockDetail1 = new BasStockDetail();
         basStockDetail1.setLotNo(basStockDetails.get(i).getLotNo());
         long l = basStockDetailMapper.countStockDetail(basStockDetail1);
         in[i] = l;
      }
      Map<String, long[]> map = new HashMap<>();
      map.put("in", in);
      return map;
   }

   @Override
   public Map<String, Long> getCargoSpaceAnalysisByRoadway(String roadway) {
      Map<String, Long> map = new HashMap<>();
      for (int i = 1; i < 7; i++) {
         Integer status = basCargoSpaceMapper.findByStatusR(i + "",roadway);
         map.put("s" + i, Long.valueOf(status));
      }
      return map;
   }

   @Override
   public Map<String, String[]> getRoadway() {
      Map<String, String[]> map = new HashMap<>();
      List<BasCargoSpace> list = basCargoSpaceMapper.findRoadway();
      String [] roadway=new String[list.size()];
      for(int i=0;i<list.size();i++){
         roadway[i] = list.get(i).getRoadway_id()+"巷道";
      }
      map.put("roadway", roadway );
      return map;
   }
   @Override
   public Map<String, String[]> getTian() {
      Map<String, String[]> map = new HashMap<>();
      String [] roadway=new String[31];
      for (int i = 0; i <31 ; i++) {
         roadway[i] = i+1+"日";
      }
      map.put("roadway", roadway);
      return map;
   }
   @Override
   public Map<String, String[]> getStocks(String startTime, String finishTime) {

      BasStockDetail basStockDetail = new BasStockDetail();
      if (startTime.equals("") && finishTime.equals("")){
         Date date = new Date();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
         String format = sdf.format(date);
         basStockDetail.setStartTime(format+"00");
      }else {
         basStockDetail.setStartTime(startTime);
         basStockDetail.setFinishTime(finishTime);
      }
      Map<String, String[]> map = new HashMap<>();
      List<BasStockDetail> basStockDetails = basStockDetailMapper.selectBasStockDetailByLoton(basStockDetail);
      String [] roadway=new String[basStockDetails.size()];
      for (int i = 0; i <basStockDetails.size() ; i++) {
         roadway[i] = basStockDetails.get(i).getLotNo();
      }
      map.put("roadway", roadway);
      return map;
   }
}
